package com.lcy.demo.annotation;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;

/**
 * Created by PC on 2017/3/29.
 */
@Configuration
@SpringBootApplication
@MapperScan("com.lcy.demo.annotation")
public class Application {

    @Bean
    public DataSource dataSource(){
        DriverManagerDataSource dataSource = new DriverManagerDataSource("jdbc:mysql://localhost:3306/demo?useSSL=true&characterEncoding=UTF-8&serverTimezone=UTC");
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setPassword("gc2017");
        dataSource.setUsername("root");
        return dataSource;
    }

    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(){
        SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
        sessionFactoryBean.setDataSource(dataSource());
        sessionFactoryBean.setTypeAliasesPackage("com.lcy.demo.annotation");
        return sessionFactoryBean;
    }

    @Bean
    public UserService userService(){
        return new UserServiceImpl();
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
